﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Business.Mst;

namespace ESOL.Business.Sec
{
    public class SecUserBs
    {
        public static List<SecUserEntity> GetAllUsers()
        {
            List<SecUserEntity> list = DataAccess.SelectAll<SecUserEntity>();
            return list;
        }

        public static List<SecUserEntity> GetAllAvailableUser()
        {
            string sql = "SELECT * FROM SEC_USER WHERE IS_DELETE != 1";
            List<SecUserEntity> list = DataAccess.Select<SecUserEntity>(sql);
            return list;
        }

        public static DataSet GetDSAllUsers()
        {
            string sql = "SELECT * FROM SEC_USER";
            DataSet ds = DataAccess.SelectDataSet(sql);
            return ds;
        }

        public static SecUserEntity GetUserByCode(string userCode)
        {
            string sql = "SELECT * FROM SEC_USER WHERE UPPER(USER_ACCOUNT) = '" + userCode.ToUpper() + "' AND IS_DELETE != 1 AND IS_LOCK != 1";
            List<SecUserEntity> list = DataAccess.Select<SecUserEntity>(sql);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            return null;
        }

        public static SecUserEntity GetUserByID(string userID)
        {
            if (string.IsNullOrEmpty(userID))
            {
                return null;
            }
            SecUserEntity user = new SecUserEntity();
            user.UserId = userID;
            return DataAccess.SelectSingle<SecUserEntity>(user);
        }

        public static SecUserEntity GetUserByPersonID(string personId)
        {
            if (string.IsNullOrEmpty(personId))
            {
                return null;
            }
            SecUserEntity user = new SecUserEntity();
            user.PersonId = personId;
            return DataAccess.SelectSingle<SecUserEntity>(user);
        }

        public static bool UpdateUser(SecUserEntity entity)
        {
            return DataAccess.Update(entity);
        }

        public static bool UpdateUsers(List<SecUserEntity> list)
        {
            return DataAccess.Update<SecUserEntity>(list);
        }

        public static bool InsertUser(SecUserEntity entity)
        {
            return DataAccess.Insert(entity);
        }

        public static bool InsertUsers(List<SecUserEntity> list)
        {
            return DataAccess.Insert<SecUserEntity>(list);
        }

        public static bool DeleteUser(SecUserEntity entity)
        {
            return DataAccess.Delete(entity);
        }

        public static bool DeleteUsers(List<SecUserEntity> list)
        {
            return DataAccess.Delete<SecUserEntity>(list);
        }

        public static DataTable SearchUserByID(string UserId)
        {
            string sqlstr = " SELECT USER_ID,ENGLISH_NAME  FROM SEC_USER WHERE 1=1  ";
            if (UserId != "All")
                sqlstr += " AND USER_ID ='" + UserId + "'";
            return DataAccess.SelectDataSet(sqlstr).Tables[0];
        }

        public static string GetUserRoleCategory(string UserId)
        {
            string sql = @"SELECT R.ROLE_CATEGORY
                            FROM SEC_ROLE R
                            INNER JOIN SEC_USER_ROLE UR ON UR.ROLE_ID = R.ROLE_ID
                            WHERE UR.USER_ID = '" + UserId + @"'
                            GROUP BY R.ROLE_CATEGORY";
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string category = "";
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["ROLE_CATEGORY"] != null)
                    {
                        category += dr["ROLE_CATEGORY"].ToString();
                    }
                }
                return category;
            }
            return "";
        }

        public static bool SavePassword(SecUserEntity user, string newPassword)
        {
            user.Password2 = user.Password1;
            user.Password1 = user.Password;
            user.Password = Common.Encryption.Encrypt(newPassword);
            user.PasswordModifyDate = DateTime.Now;
            return DataAccess.Update(user);
        }

        public static List<MstPersonEntity> GetPersonIdByFormAndObject(string sFormName, string sObjectName, string sObjectCode)
        {
            string sqlstr = " select PERSON_ID from dbo.SEC_USER where USER_ID in ";
                   sqlstr +="(select USER_ID from dbo.SEC_USER_ROLE where ROLE_ID in ";
                   sqlstr += "(select ROLE_ID from dbo.SEC_OBJECT_ROLE where OBJECT_ID in ";
                   sqlstr += "(select OBJECT_ID from dbo.SEC_OBJECT where MENU_ID in ";
                   sqlstr += "(select MENU_ID from dbo.SEC_MENU where FORM_NAME='" + sFormName + "') and OBJECT_NAME='" + sObjectName + "' and OBJECT_CODE='" + sObjectCode + "')))  ";

            DataTable dt= DataAccess.SelectDataSet(sqlstr).Tables[0];

            List<MstPersonEntity> toList = new List<MstPersonEntity>();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MstPersonEntity person = MstPersonBs.GetMstPersonByID(dt.Rows[i][0].ToString());
                    toList.Add(person);
                }
            }
            return toList;
        }

    }
}
